<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/17
 * Time: 9:48
 */
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\UserBind;
use think\Db;

class Funds extends Api {
	
	protected $noNeedRight = ['*'];
	
	public function index(){
		$userId = $this->auth->id;
		$targetUserId = input('post.user_id', '', 'intval');
		$page = input('post.page', 1, 'intval');
		
		//如果有指定特定用户ID，查看指定用户的帐户
		if($targetUserId) {
			$where['f.user_id'] = $targetUserId;
		} else {
			//默认查看自己和绑定用户的帐户
			$bindUsers = Db::name('userBind')
				->where(['user_id' => $userId, 'status' => UserBind::STATUS_Y])
				->column('target_user_id', 'id');
			$bindUsers[] = $userId;
			$where['f.user_id'] = ['in', $bindUsers];
		}
		
		$date = input('post.date', '');
		if($date) {
			$startTime = strtotime($date);
			$endTime = $startTime + 24*3600;
			$where['f.date'] = [['egt', $startTime], ['lt', $endTime]];
		}
		
		$accountId = input('post.account_id');
		$accountId && $where['f.account_id'] = $accountId;
		
		$type = input('post.operation_type');
		$type && $where['f.operation_type'] = $type;
		
		
		$model = new \app\common\model\Funds();
		$rows = $model->alias('f')
			->join('userAccount a', 'f.account_id = a.id', 'left')
			->join('fundsType t', 'f.funds_type_id = t.id', 'left')
			->join('userBind u', 'f.user_id = u.target_user_id and u.user_id = '.$userId, 'left')
			->where($where)
			->field('f.id, f.user_id, f.operation_type, f.amount, from_unixtime(f.date, "%Y-%m-%d") as create_time, f.remark, f.address, a.name as account_name, t.name as type_name, t.icon_image, u.remark as user_remark')
			->order(['f.date' => 'DESC', 'f.create_time' =>'DESC'])
			->page($page)
			->select();
		
		$inAmount = 0;
		$outAmount = 0;
		
		$count = $model->alias('f')
			->where($where)
			->group('f.operation_type')
			->field('f.operation_type, sum(f.amount) as amount')
			->select();
		
		if($count) {
			foreach($count as $row) {
				if($row['operation_type'] == $model::OPERATION_TYPE_LESS) {
					$outAmount = $row['amount'];
				} else {
					$inAmount = $row['amount'];
				}
			}
		}
		
		$returnData = [
			'list' => $rows,
			'inAmount' => $inAmount,
			'outAmount' => $outAmount,
		];
		
		$this->success('查询成功', $returnData);
	}
	
	
	/**
	 * 添加资金流水
	 */
	public function add(){
		$data = input('post.');
		$data['user_id'] = $this->auth->id;
		$vali = new \app\common\validate\Funds();
		if(!$vali->check($data)) {
			$this->error($vali->getError());
		}
		
		$model = new \app\common\model\Funds();
		$data['date'] = strtotime($data['date']);
		
		if($data['id']) {
			$res = $model->editRecord($data);
			$msg = '修改成功';
		} else {
			$res = $model->addRecord($data);
			$msg = '添加成功';
		}
		if($res) {
			$this->success($msg);
		} else {
			$this->error('操作出错，请稍候重试'.$model->getError());
		}
	}
	
	public function edit(){
	
	}
	
	
	/**
	 * 流水详情
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function detail(){
		$id = input('post.id');
		$userId = $this->auth->id;
		
		$model = new \app\common\model\Funds();
		$rows = $model->alias('f')
			->join('userAccount a', 'f.account_id = a.id', 'left')
			->join('fundsType t', 'f.funds_type_id = t.id', 'left')
			->join('userBind u', 'f.user_id = u.target_user_id and u.user_id = '.$userId, 'left')
			->where(['f.id' => $id])
			->field('f.id, f.user_id, f.operation_type, f.amount, from_unixtime(f.date, "%Y-%m-%d") as date, from_unixtime(f.create_time) as create_time, f.remark, f.address, f.before_amount, f.after_amount, a.name as account_name, t.name as type_name, t.icon_image, u.remark as user_remark, f.account_id, f.funds_type_id, a.name as account_text')
			->find();
		
		$this->success('查询成功', $rows);
	}
}